﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using AMStudio.Windows.Forms;
using AMStudio.DelegateDispather;
using AMStudio.Common;

namespace AMStudio.UserInterface
{
    public partial class StationUI01 : StationUI
    {
        public StationUI01()
        {
            this.UIName = "StationUI01";
            this.StationIndex = 1;
            InitializeComponent();
        }

        public override bool ActivateUI()
        {
            bool result = base.ActivateUI();
            this.GetProductInformation();
            return result;
        }

        private void GetProductInformation()
        {
            if (WorkorderDB.ActiveProduct != null)
            {
                this.txtProductName.Text = WorkorderDB.ActiveProduct.Name.ToString();
                this.txtNestID.Text = WorkorderDB.ActiveProduct.St1NestID.ToString();
                this.txtHousingRequiredM.Text = WorkorderDB.ActiveProduct.St1HousingSNRequirement;
                this.txtConnectorRequired.Text = WorkorderDB.ActiveProduct.St1ConnectorSNRequirement;
            }
        }

        protected override void BackGroud_EventHandler(object sender, StationEventArgs e)
        {
            base.BackGroud_EventHandler(sender, e);          
            if (e.From == MachineConfig.Default.St1HousingScannerConfig.HardwareName)
            {
                if (e.Command == "DATA_RECEIVE")
                {
                    this.txtCurrentHousingSN.Text = e.Value.ToString();
                    if (string.Compare(this.txtCurrentHousingSN.Text, this.txtHousingRequiredM.Text, true) == 0)
                    {
                        simHousingSNRequiredM.ForeColor = System.Drawing.Color.Green;
                    }
                    else
                    {
                        simHousingSNRequiredM.ForeColor = System.Drawing.Color.DarkGray;
                    }
                }
            }
            else if (e.From == MachineConfig.Default.St1ConnecterScannerConfig.HardwareName)
            {
                if (e.Command == "DATA_RECEIVE")
                {
                    this.txtCurrentConnectorSN.Text = e.Value.ToString();
                    if (string.Compare(this.txtCurrentConnectorSN.Text, this.txtConnectorRequired.Text, true) == 0)
                    {
                        simConnector.ForeColor = System.Drawing.Color.Green;
                    }
                    else
                    {
                        simConnector.ForeColor = System.Drawing.Color.DarkGray;
                    }
                }
            }
            else if (e.From == MachineConfig.Default.St1CameraConfig.HardwareName)
            {
                if (e.Command == "TRIGGER_RESULT")
                {
                    string rst = e.Value.ToString();
                    switch (rst)
                    {
                        case "1":
                            {
                                this.simCamera.ForeColor = System.Drawing.Color.Green;
                            }
                            break;
                        case "0":
                            {
                                this.simCamera.ForeColor = System.Drawing.Color.Red;
                            }
                            break;
                        case "":
                            {
                                this.simCamera.ForeColor = System.Drawing.Color.DarkGray;
                            }
                            break;
                    }
                }
            }
            else if (e.From == "MANAGER")
            {
                switch (e.Command)
                {
                    case "ACTIVE_ORDER":
                        this.GetProductInformation();
                        break;
                }
            }
        }

        private void MyPanel_Click(object sender, EventArgs e)
        {

        }

        private void StationUI01_Load(object sender, EventArgs e)
        {
            base.SubscribeEvent("MANAGER_RET");
            base.SubscribeEvent(MachineConfig.Default.St1HousingScannerConfig.HardwareEvent);
            base.SubscribeEvent(MachineConfig.Default.St1ConnecterScannerConfig.HardwareEvent);
            base.SubscribeEvent(MachineConfig.Default.St1CameraConfig.HardwareEvent);
        }        



        protected override void SetUserPrivilige(object sender, EventArgs e)
        {
            base.SetUserPrivilige(sender, e);
            User current = UserDB.CurrentLoginUser;
            //Control[] findCtl = null;
            switch (current.AuthorizationSet)
            {
                case 0:
                    this.tabItemMaintenace.Visible = true;
                    this.tabItemConfiguration.Visible = true;
                    this.tabItemRFTag.Visible = true;
                    this.tabItemIput.Visible = true;
                    this.tabItemOutput.Visible = true;
                    break;
                case 1:
                    this.tabItemMaintenace.Visible = true;
                    this.tabItemConfiguration.Visible = false;
                    this.tabItemRFTag.Visible = false;
                    this.tabItemIput.Visible = false;
                    this.tabItemOutput.Visible = false;
                    break;
                case 2:
                default:
                    this.tabItemMaintenace.Visible = false;
                    this.tabItemConfiguration.Visible = false;
                    this.tabItemRFTag.Visible = false;
                    this.tabItemIput.Visible = false;
                    this.tabItemOutput.Visible = false;
                    break;
            }
        }
        protected override void Language_RfreshTag(LanguageItem item)
        {
            base.Language_RfreshTag(item);


            if ((item != null) && (item.Language[LanguageString] != null))
            {
                if (item.ControlName == "tabStrip1")
                {

                    string[] titles = item.Language[LanguageString].ToString().Split(new string[] { "||" }, StringSplitOptions.None);

                    if (titles.Length >= 6)
                    {
                        this.tabItemStatus.Text = titles[0];
                        this.tabItemIput.Text = titles[1];
                        this.tabItemOutput.Text = titles[2];
                        this.tabItemMaintenace.Text = titles[3];
                        this.tabItemConfiguration.Text = titles[4];
                        this.tabItemRFTag.Text = titles[5];

                    }
                }
            }
        }
        protected override void Language_InitializeControl(Control UIControl)
        {
            base.Language_InitializeControl(UIControl);

            if (UIControl is StationUI)
            {
                LanguageItem item = LanguageDB.UIDBTags[this.UIName + ".tabStrip1"] as LanguageItem;
                if ((item != null) && (item.Language[LanguageString] != null))
                {
                    string[] titles = item.Language[LanguageString].ToString().Split(new string[] { "||" }, StringSplitOptions.None);

                    if (titles.Length >= 6)
                    {
                        this.tabItemStatus.Text = titles[0];
                        this.tabItemIput.Text = titles[1];
                        this.tabItemOutput.Text = titles[2];
                        this.tabItemMaintenace.Text = titles[3];
                        this.tabItemConfiguration.Text = titles[4];
                        this.tabItemRFTag.Text = titles[5];
                    }
                }
            }

           
        }
    }
}
